//
// Created by 32513 on 25-4-9.
//

#include "transmission.h"

#include <string.h>
#include <usart.h>

#include "stm32f4xx_hal_uart.h"

#include "rc_sbus.h"


/*********串口1数据缓冲区和标志位********/
uint8_t Rx1buffer[3];
uint8_t data1;
uint8_t cnt1;
uint8_t
Rx2buffer[3];

/*********串口2数据缓冲区和标志位********/

uint8_t data2;
uint8_t cnt2;
uint8_t data2_head_flag;


/**********mv通信情况**********/

uint32_t mv1_last_data_time = 0;
float mv1_alpha = 0;
float mv1_front_speed = 0;
int8_t mv1_uart_live = 0;
int16_t Ball_Y_Val = 0;
int16_t Last_Ball_Y_Val = 0;



uint32_t mv2_last_data_time = 0;
float mv2_alpha = 0;
float mv2_front_speed = 0;
int8_t mv2_uart_live = 0;
int16_t Kuang_Y_Val = 0;
int16_t Last_Kuang_Y_Val = 0;



uint32_t SBUS_last_data_time = 0;
int8_t SBUS_uart_live = 0;


void TransmissionUart_Init()
{
    HAL_UART_Receive_IT(&huart5,&data1,1);
    __HAL_UART_ENABLE_IT(&huart5,UART_IT_RXNE);	//使能空闲中断
    HAL_UART_Receive_IT(&huart4,&data2,1);
    __HAL_UART_ENABLE_IT(&huart4,UART_IT_RXNE);	//使能空闲中断


    /*******SBUS接受串口初始化******/
    MX_USART2_UART_Init();

    HAL_UART_Receive_IT(&huart2,SBUS_Row_Data,100);
    __HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);

}

void Check_UART_Transmission()
{



    if(HAL_GetTick() - mv1_last_data_time > 500 /*|| Rx1buffer[0] != 0x91*/)
    {
        MX_USART1_UART_Init();
        HAL_UART_Receive_IT(&huart5,Rx1buffer,1);
        __HAL_UART_ENABLE_IT(&huart5,UART_IT_RXNE);	//使能空闲中断
        mv1_uart_live = 0;
    }
    else
        mv1_uart_live = 1;

    if(HAL_GetTick() - mv2_last_data_time > 500 /*|| Rx2buffer[0] != 0x71*/)
    {
        // MX_UART4_Init();
        // HAL_UART_Receive_IT(&huart4,Rx2buffer,1);
        // __HAL_UART_ENABLE_IT(&huart4,UART_IT_RXNE);	//使能空闲中断
        mv2_uart_live = 0;
    }
    else
        mv2_uart_live = 1;


    if(HAL_GetTick() - SBUS_last_data_time > 1000 /*|| Rx2buffer[0] != 0x71*/)
    {
        NVIC_SystemReset();
        MX_USART2_UART_Init();
        HAL_UART_Receive_IT(&huart2,SBUS_Row_Data,100);
        __HAL_UART_ENABLE_IT(&huart2,UART_IT_RXNE);	//使能空闲中断
        SBUS_uart_live = 0;
    }
    else
        SBUS_uart_live = 1;
}


//串口中断回调，处理openmv数据

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if(huart->Instance == UART5)
    {
        // �?测到起始字节 0x91
        if (data1 == 0x91) {
            cnt1 = 0;                         // 重置计数�?
            Rx1buffer[cnt1++] = data1;        // 存储起始字节并�?�增
        }
        // 正在接收数据帧（起始字节已确认）
        else if (cnt1 > 0 && cnt1 < 3)
        {
            Rx1buffer[cnt1++] = data1;       // 存储数据并�?�增
        }
        cnt1 %= 3;
        mv1_last_data_time = HAL_GetTick();
        mv1_alpha = (int16_t)Rx1buffer[1] - 80;
        Ball_Y_Val = Rx1buffer[2];
        mv1_front_speed = (255.0f - (float)Rx1buffer[2])/255.0f*5.0f;
        HAL_UART_Receive_IT(&huart5,&data1,1);
    }
    if(huart->Instance == UART4)
    {
        // �?测到起始字节 0x71
        if (data2 == 0x71) {
            cnt2 = 0;                         // 重置计数�?
            Rx2buffer[cnt2++] = data2;        // 存储起始字节并�?�增
            mv2_last_data_time = HAL_GetTick();

        }
        // 正在接收数据帧（起始字节已确认）
        else if (cnt2 > 0 && cnt2 < 3)
        {
            Rx2buffer[cnt2++] = data2;       // 存储数据并�?�增
        }
        // 完整帧接收完�?
        cnt2 %= 3;
        mv2_alpha = (int16_t)Rx2buffer[1] - 80;
        Kuang_Y_Val = Rx2buffer[2];
        mv2_front_speed = (255.0f - (float)Rx2buffer[2])/255.0f*5.0f;
        HAL_UART_Receive_IT(&huart4,&data2,1);
    }

    if(huart->Instance == USART2)
    {
        memcpy(SBUS_ROW_Data_Temp,SBUS_Row_Data,100);
        for (int i = 0; i<25; i++)
        {
            if(SBUS_ROW_Data_Temp[i] == 0x0F )
            {
                memcpy(sbus_rx_buf,SBUS_ROW_Data_Temp+i,25);
                sbus_rc_decode(sbus_rx_buf);
                                SBUS_last_data_time = HAL_GetTick();

                break;
            }
        }

        HAL_UART_Receive_IT(&huart2,SBUS_Row_Data,100);
    }
}